package com.dragonsky.shop.user.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dragonsky.shop.common.utils.AjaxResult;
import com.dragonsky.shop.user.config.UserNaCosConfig;
import com.dragonsky.shop.user.domain.vo.StarListVo;
import com.dragonsky.shop.user.domain.vo.UserNaCosVo;
import com.dragonsky.shop.user.entity.User;
import com.dragonsky.shop.user.mapper.UserMapper;
import com.dragonsky.shop.user.sentinel.handler.UserServiceSentinelHandler;
import com.dragonsky.shop.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author dragon
 * @since 2025-03-22 00:13:28
 */
@Service
@RefreshScope
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Value("${user.soldValue}")
    private String soldValue;
    @Resource
    private UserNaCosConfig userNaCosConfig;

    @Resource
    private UserMapper userMapper;


    @Override
    public String test() {
        User user = User
            .builder()
            .build();
        return "";
    }

    @Override
    public AjaxResult<UserNaCosVo> getNaCosConfig() {
        return AjaxResult.succeedWith(UserNaCosVo
            .builder()
            .name(userNaCosConfig.getName())
            .startTime(userNaCosConfig.getStartTime())
            .endTime(userNaCosConfig.getEndTime())
            .soldValue(soldValue)
            .build()
        );
    }

    @Override
    @SentinelResource(value = "starListSentinel", blockHandlerClass = UserServiceSentinelHandler.class, blockHandler = "getStarListBlockHandler", fallbackClass = UserServiceSentinelHandler.class, fallback = "getStarListFallbackHandler")
    public AjaxResult<List<StarListVo>> getStarList() {
        Random random = new Random();
        if (random.nextInt(10) + 1 > 5) {
            throw new RuntimeException("获取明星列表出错了");
        }

        List<StarListVo> starListVos = new ArrayList<>();
        starListVos.add(StarListVo.builder().name("周慧敏").age(18).build());
        starListVos.add(StarListVo.builder().name("关之琳").age(20).build());
        starListVos.add(StarListVo.builder().name("王祖贤").age(21).build());

        return AjaxResult.succeedWith(starListVos);
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public AjaxResult<Void> seata() throws InterruptedException {
        userMapper.insert(User
            .builder()
                .userName("seata")
                .balance(BigDecimal.ZERO)
                .status(Byte.valueOf("0"))
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
            .build()
        );

        Random random = new Random();
        int i2 = random.nextInt(10) + 1;
        if (i2 > 5) {
            Thread.sleep(3000);
            throw new RuntimeException("用户数据需要回滚");
        }

        return AjaxResult.succeed();
    }

}
